// 创建一个路由器，并暴露出去
// 第一步：引入createRouter
import {createRouter, createWebHistory} from 'vue-router'
// 引入一个一个可能要呈现组件
import Login from '@/views/Login.vue'
import Index from '@/views/Index.vue'
import Admin from '@/views/Admin.vue'
import Product from '@/views/Product.vue'
import Order from '@/views/Order.vue'
import Catetory from '@/views/Category.vue'

//创建路由器
const router = createRouter({
    history: createWebHistory(),
    routes: [
        {path: '/login', component: Login},
        {
            path: '/', component: Index, children: [
                {path: '/admin', component: Admin},
                {path: '/product', component: Product},
                {path: '/order', component: Order},
                {path: '/category', component: Catetory}
            ]
        }
    ]
})

//https://v3.router.vuejs.org/zh/guide/advanced/navigation-guards.html#%E5%85%A8%E5%B1%80%E5%89%8D%E7%BD%AE%E5%AE%88%E5%8D%AB
//路由守卫
//全局前置守卫
import {useTokenStore} from '@/store/token.js'
import {ElMessage} from "element-plus";
let whiteList = ['/login']; // 白名单
router.beforeEach((to, from, next) => {
    const tokenStore = useTokenStore()
    const token = tokenStore.token;
    if (!whiteList.includes(to.path) && !token) {
        next('/login')
    } else {
        next()
    }
})

// 暴露出去router
export default router